package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 购物车列表查询
     *
     * @return
     */
    @GetMapping("/list")
    public R list() {
        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper<>();
        //根据当前用户查询
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //排序
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        //执行查询
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        return R.success(shoppingCartList);
    }

    /**
     * 将商品添加至购物车
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        Long currentId = BaseContext.getCurrentId();//当前登陆用户
        //1. 查看当前登陆用户有没有添加过菜品或者套餐
        //select * from shopping_cart where user_id=? and setmeal_id =?
        //select * from shopping_cart where user_id=? and dish_id =?
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCartDB = shoppingCartService.getOne(lqw);
        if(shoppingCartDB != null) {
            //2. 如果有 数量+1
            shoppingCartDB.setNumber(shoppingCartDB.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartDB);
            //返回购物车信息
            return R.success(shoppingCartDB);
        } else{
            //3. 如果没有 直接添加
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    /**
     * 删减购物车的商品信息
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R delete(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();//当前登陆用户
        //根据id找到购物车里的商品
        shoppingCart.getId();
        //1. 查看当前登陆用户有没有添加过菜品或者套餐
        //select * from shopping_cart where user_id=? and setmeal_id =?
        //select * from shopping_cart where user_id=? and dish_id =?
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCartDB = shoppingCartService.getOne(lqw);
        if(shoppingCartDB.getNumber() > 1) {
            shoppingCartDB.setNumber(shoppingCartDB.getNumber() - 1);
            shoppingCartService.updateById(shoppingCartDB);
            return R.success(shoppingCart);
        }else{
            shoppingCartService.removeById(shoppingCartDB);
            return R.success(shoppingCartDB);
        }
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //根据当前用户删除
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }
}